package com.taxshare.tools.util.sst;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.*;

public class HttpUtil {

	private HttpUtil() {
		throw new IllegalStateException("Utility class");
	}

	/**
	 * pose方式请求
	 * @param url
	 * @return {statusCode : "请求结果状态代码", responseString : "请求结果响应字符串"}
	 */
	public static Map post(String url, Map<String, String> params) {
		DefaultHttpClient httpclient = new DefaultHttpClient();
		Map reponseMap = new HashMap();
		try {
			HttpPost post = postForm(url, params);
			httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);
			httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 600000);
			reponseMap = invoke(httpclient, post);
		}catch (Exception e){
			e.printStackTrace();
		}finally {
			httpclient.getConnectionManager().shutdown();
			httpclient.close();
		}

		return reponseMap;
	}
	

	public static Map postJson(String url, String json) throws UnsupportedEncodingException{
		DefaultHttpClient httpclient = new DefaultHttpClient();
		StringEntity s;
		Map reponseMap = null;
		s = new StringEntity(json.toString(), "UTF_8");
		s.setContentEncoding("UTF_8");
		s.setContentType("application/json; charset=utf-8");
		HttpPost post = new HttpPost(url);
		reponseMap = invoke(httpclient, post);
		httpclient.getConnectionManager().shutdown();
		return reponseMap;
	}

	public static Map postJson(String url, JSONObject json) throws UnsupportedEncodingException {
		DefaultHttpClient httpclient = new DefaultHttpClient();
		StringEntity s;
		Map reponseMap = null;
		HttpPost httpPost = new HttpPost(url);
		StringEntity entity = new StringEntity(json.toString(), "UTF_8");
		entity.setContentEncoding("UTF_8");
		entity.setContentType("application/json");
		httpPost.setEntity(entity);
		reponseMap = invoke(httpclient, httpPost);
		httpclient.getConnectionManager().shutdown();
		return reponseMap;
	}


    private static HttpPost postForm(String url, Map<String, String> params){
		HttpPost httpost = new HttpPost(url);
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		Set<String> keySet = params.keySet();
		for(String key : keySet) {
			nvps.add(new BasicNameValuePair(key, params.get(key)));
		}
		try {
			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return httpost;
	}


    private static Map invoke(DefaultHttpClient httpclient,
                              HttpUriRequest httpost) {
		Map returnMap = new HashMap();
		HttpResponse response = sendRequest(httpclient, httpost);
		String body="";
		try{
			body = paseResponse(response);
			returnMap.put("statusCode", response.getStatusLine().getStatusCode());	// 请求返回结果状态
			returnMap.put("response", body);
		}catch (Exception e){
			e.printStackTrace();
		}

		return returnMap;
	}


    private static HttpResponse sendRequest(DefaultHttpClient httpclient,
                                            HttpUriRequest httpost) {
		HttpResponse response = null;
		try {
			response = httpclient.execute(httpost);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return response;
	}

    private static String paseResponse(HttpResponse response) {
		HttpEntity entity = response.getEntity();
		String body = null;
		try {
			body = EntityUtils.toString(entity,"UTF-8");
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return body;
	}
    
}
